package cn.edu.zju.aws.s3.demo;

import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.UUID;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class S3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		AWSCredentials credent=new ProfileCredentialsProvider("default").getCredentials();
		AmazonS3 s3=new AmazonS3Client(credent);
        Region usWest2 = Region.getRegion(Regions.US_WEST_2);
        s3.setRegion(usWest2);		
        
        String bucketname="chihyung";
        
        //get buckets
        java.util.List<Bucket> bucket=s3.listBuckets();
        
        
        for(Bucket buck:bucket){
        	System.out.println("bucket's name:"+buck.getName());
        }
        
        //add buckets
//        String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
//        Bucket buck=s3.createBucket(bucketName);
//        System.out.println(buck.getName());
        
        //delete buckets
        
//        s3.deleteBucket("my-first-s3-bucket-7ddd2286-5990-4de5-92ff-df8a50b39c50");
        
        //add file to buckets
//        String key = "file";
//        try {
//			s3.putObject(new PutObjectRequest("chihyung", key, createSampleFile()));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
        
        //get file from bucket
        S3Object file= s3.getObject(new GetObjectRequest(bucketname,"file"));
        System.out.println(getStringFromStream(file.getObjectContent()));
        
        
        //get all files from buckets
        ObjectListing files=s3.listObjects(new ListObjectsRequest().withBucketName("chihyung"));
        for(S3ObjectSummary ob:files.getObjectSummaries()){
        	System.out.println(ob.getKey());
        }
        
        //delete file form buckets
        s3.deleteObject(new DeleteObjectRequest("chihyung", "file"));
	}
	
	
    public static File createSampleFile() throws IOException {
        File file = File.createTempFile("aws-java-sdk-", ".txt");
        file.deleteOnExit();

        Writer writer = new OutputStreamWriter(new FileOutputStream(file));
        writer.write("abcdefghijklmnopqrstuvwxyz\n");
        writer.write("01234567890112345678901234\n");
        writer.write("!@#$%^&*()-=[]{};':',.<>/?\n");
        writer.write("01234567890112345678901234\n");
        writer.write("abcdefghijklmnopqrstuvwxyz\n");
        writer.close();

        return file;
    }
    
    public static String getStringFromStream(InputStream in)
    {
    	BufferedReader br=new BufferedReader(new InputStreamReader(in));
    	String line=null;
    	StringBuffer sb=new StringBuffer();
    	while(true)
    	{
    		try {
				line=br.readLine();
				if(line==null)break;
				sb.append(line);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		
    	}
    	return sb.toString();
    	
    }
}
